from user import *
from django.template import Template

def Register(request):
    register_t = loader.get_template('core/_register.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(register_t.render(c))

def Template(request,sname):
    register_t = loader.get_template('core/_template.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(register_t.render(c))

def Color(request,sname):
    register_t = loader.get_template('core/_color.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(register_t.render(c))

def ChangePWD(request):
    register_t = loader.get_template('core/_change_pwd.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(register_t.render(c))

def AddService(request):
    form_t = loader.get_template('core/_addservice.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(form_t.render(c))

def ModifyService(request,sname):
    form_t = loader.get_template('core/_mdyservice.html')
    try:
      service = ServiceCore.objects.get(name = sname)
      c = Context({"REQUEST":request.REQUEST,"SERVICE":service})
      return HttpResponse(form_t.render(c))
    except ServiceCore.DoesNotExist:
      return HttpResponse("Service Does Not Exist") 


def ModifyDescription(request,sname):
    form_t = loader.get_template('core/_mdydescription.html')
    try:
      service = ServiceCore.objects.get(name = sname)
      c = Context({"REQUEST":request.REQUEST,"SERVICE":service})
      return HttpResponse(form_t.render(c))
    except ServiceCore.DoesNotExist:
      return HttpResponse("Service Does Not Exist") 


# added by mike for special event management
#def SpecialEventService(request,sname):
#    form_t = loader.get_template('core/_mdyspecialevent.html')
#    try:
#      special_event = SepcialEvent.objects.get(name = sname)
#      c = Context({"REQUEST":request.REQUEST,"SERVICE":service})
#      return HttpResponse(form_t.render(c))
#    except SepcialEvent.DoesNotExist
#      return HttpResponse("Special Event Not Exist")

def AddPost(request,sname):
    form_t = loader.get_template('core/_addpost.html')
    c = Context({"REQUEST":request.REQUEST,"SERVICE":sname})
    return HttpResponse(form_t.render(c))

def AddRoster(request,sname,day):
    form_t = loader.get_template('core/_addroster.html')
    c = Context({"DAY":int(day),"SERVICE":sname})
    return HttpResponse(form_t.render(c))

def AddRates(request,sname,day):
    form_t = loader.get_template('core/_addrates.html')
    c = Context({"DAY":int(day),"SERVICE":sname})
    return HttpResponse(form_t.render(c))

def ModifyTimeTable(request):
    form_t = loader.get_template('core/_mdytimetable.html')
    c = Context({})
    return HttpResponse(form_t.render(c))


def Login(request):
    login_t = loader.get_template('core/_login.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(login_t.render(c))

def AboutUs(request):
    aboutUs_t = loader.get_template('core/_aboutus.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(aboutUs_t.render(c))

def AddGallery(request,sname):
    addgallery_t = loader.get_template('core/_addgallery.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(addgallery_t.render(c))

def ModifyIcon(request,sname):
    aut = HasAuthority(request,sname)
    command_error = {}
    if (aut['r'] == False):
      command_error['AUTHORITY'] = 'NO_AUTHORITY'
      return GeneralXMLResponse(request,command_error,"No Authority")
    else:
      mdy_icon_t = loader.get_template('core/_mdy_icon.html')
      dic = {'ITEM':aut['s'].GetItem('icon')}
      gnode = GetConfigDoc(sname)
      gallery = Gallery.InitGalleryConfig(gnode)
      basicinfo = gallery.BasicInfo()
      dic['GALLERY'] = basicinfo
      c = Context(dic)
      return HttpResponse(mdy_icon_t.render(c),mimetype="text/html")

def ModifyDefaultGallery(request,sname):
    mdy_t = loader.get_template('core/_mdydefaultgallery.html')
    service = GetService(sname)
    if(service):
        gnode = etree.parse(CONFIG.SERVICES_PATH + sname+'/config.xml')
        gallery = Gallery.InitGalleryConfig(gnode.getroot())
        c = Context({'GALLERYS':json.dumps(gallery.BasicInfo().keys()),'DEFAULT':'None'})
        return HttpResponse(mdy_t.render(c),mimetype="text/html")
    else:
        return HttpResponse("Service %s not exist"%(sname))

def Resource(request,sname,respath):
    aut = HasAuthority(request,sname)
    command_error = {}
    if (aut['r'] == False):
      command_error['AUTHORITY'] = 'NO_AUTHORITY'
      return GeneralXMLResponse(request,command_error,"No Authority")
    else:
      dic = {}
      path = respath.split('/')
      resname = path.pop()
      gname = ''
      if path:
        gname = path.pop()
      else:
        command_error['INVALID'] = "INVALID GALLERY NAME"
        return GeneralXMLResponse(request,command_error,'No Authority')
      gnode = GetConfigDoc(sname)
      gallery = Gallery.InitGalleryConfig(gnode)
      file = gallery.GetImageFile(gname,resname)
      if file:
        url = gname + '/' + file 
        fd = open(CONFIG.SERVICES_PATH + sname+'/'+url,'r')
        ns = file.split('.')
        image_type = ns[len(ns)-1]
        data = fd.read()
        fd.close()
        dic['USETMP'] = 'STRICT'
        if(request.REQUEST.has_key('usetmp')):
          pass
        else:
          SaveCacheData(sname,data,image_type)
      else:
        if(request.REQUEST.has_key('usetmp')):
          dic['USETMP'] = 'STRICT'
  #identify the type of gallery(03/09/2012)
      if(gname=='shop'):
        res_t = loader.get_template('core/image_shop_add.html') 
      elif(gname=='banner'):
        res_t = loader.get_template('core/image_banner_add.html')   
      else:
        res_t = loader.get_template('core/image_girl_add.html')
        #end (03/09/2012)
      dic['SERVICE'] = sname
      dic['GALLERY'] = gname
      dic['NAME'] = resname
      ua = request.META.get('HTTP_USER_AGENT', '').lower()
      if ua.find("ipad") > 0:
        dic['IPAD'] = "ipad"
      if request.REQUEST.has_key('action'):
        dic['ACTION'] = request.REQUEST['action']
      else:
        dic['ACTION'] = 'default'
      c = Context(dic)
      return HttpResponse(res_t.render(c))

